package com.sne.airways.basecore.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: 卞京帅{bjslogin#gmail.com}
 * Date: 12-11-11
 * Time: 下午10:10
 * To change this template use File | Settings | File Templates.
 */
public class FileUtils {

    /**
     * 复制文件
     * @param oldFile 完整的源文件路径+文件名
     * @param newFile 完整的目标文件名
     * @throws IOException IO
     */
    public static void copyFile(String oldFile,String newFile) throws IOException {
        int byteread = 0;
        File oldfile = new File(oldFile);
        if(oldfile.exists()){
            FileInputStream in = new FileInputStream(oldFile);
            FileOutputStream out = new FileOutputStream(newFile);
            byte[] buffer = new byte[1000];
            while((byteread = in.read(buffer)) != -1){
                out.write(buffer,0,byteread);
            }
            in.close();
        }
    }

    public static void copyFile(String oldPath,String newPath,String fileName) throws IOException{
        File o = new File(oldPath);
        File n = new File(newPath);
        if(o.exists()){
            if(!n.exists()) n.mkdirs();
            copyFile(oldPath+fileName,newPath+fileName);
        }
    }

    /**
     *删除某个文件
     * @param file 文件绝对路径+文件名
     */
    public static void deleteFile(String file){
        File f = new File(file);
        f.delete();
    }

    /**
     * 删除一个文件夹下面的全部文件及文件夹
     * @param fileDirectory  文件夹绝对路径
     */
    public static void deleteFileDirctory(String fileDirectory){
        File f = new File(fileDirectory);
        if(!f.exists()){
            return ;
        }
        File[] files = f.listFiles();
        for(File file : files){
            if(file.isFile()) {
                file.delete();
            }else{
                if(file.list().length>0){
                    deleteFileDirctory(file.getAbsolutePath());
                    file.delete();
                }else{
                    file.delete();
                }
            }
        }
    }

    /**
     * 重命名文件
     * @param oldpath 原文件
     * @param newpath 新文件
     */
    public static void renameFile(String oldpath,String newpath){
        File f = new File(oldpath);
        File newf = new File(newpath);
        f.renameTo(newf);
    }

    /**
     * 上传文件
     * @param file
     * @param directory
     * @throws IOException
     */
    public static void uploadFile(MultipartFile file,String directory) throws IOException{
        String fileName = file.getOriginalFilename();
        File targetFile = new File(directory,fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        file.transferTo(targetFile);
    }

    /**
     * 上传文件并重命名
     * @param file
     * @param directory
     * @param fileName
     * @throws IOException
     */
    public static void uploadFile(MultipartFile file,String directory,String fileName) throws IOException{
        File targetFile = new File(directory,fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        file.transferTo(targetFile);
    }

}
